using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using FireStarter.Base;
using FireStarterModeller.BO;

namespace FireStarter.CodeGeneration
{
    internal class CodeDomBoClassStub: CodeDomBase
    {
        
        protected readonly IModelClass _modelClass;

        public CodeDomBoClassStub(CodeDomProvider codeDomProvider, IModelClass defClass)
            : base(codeDomProvider)
        {
            _modelClass = defClass;
        }

        protected override CodeCompileUnit GetCodeCompileUnit()
        {
            return GetClassStubPartialCompileUnit("");
        }

        protected CodeCompileUnit GetClassStubPartialCompileUnit(string comment)
        {
            CodeNamespace classNamespace;
            CodeTypeDeclaration classType;
            return GetClassStubPartialCompileUnit(comment, false, out classNamespace, out classType);
        }

        protected CodeCompileUnit GetClassStubPartialCompileUnit(string comment, bool includeSuperclass,
                                                                 out CodeNamespace classNamespace, out CodeTypeDeclaration classType)
        {
            CodeCompileUnit classCode = new CodeCompileUnit();
            string namespaceName = _modelClass.AssemblyName ?? "Classes";
           
            classNamespace = new CodeNamespace(namespaceName);
            classNamespace.Imports.Add(new CodeNamespaceImport("System"));
            classNamespace.Imports.Add(new CodeNamespaceImport("Habanero.BO"));
            classCode.Namespaces.Add(classNamespace);
            comment = BuildFileComment(comment);
            classNamespace.Comments.Clear();
            classNamespace.Comments.Add(new CodeCommentStatement(comment));
            classNamespace.Comments.Add(new CodeCommentStatement(""));
            classType = new CodeTypeDeclaration(_modelClass.ClassName);
            classNamespace.Types.Add(classType);
            if (includeSuperclass)
            {
                string superClassName = _modelClass.SuperClassName;
                string superClassAssembly = _modelClass.SuperClassAssemblyName;
               
                bool hasSuperclass = !String.IsNullOrEmpty(superClassName);
                if (!hasSuperclass)
                {
                    superClassName = "BusinessObject";
                    superClassAssembly = "Habanero.BO";
                }
                classType.BaseTypes.Add(superClassName);
                if (!String.IsNullOrEmpty(superClassAssembly))
                {
                    EnsureAssemblyImported(classNamespace, superClassAssembly);
                    //classNamespace.Imports.Add(new CodeNamespaceImport(superClassAssembly));
                }
            }
            classType.TypeAttributes = TypeAttributes.Public;
            classType.IsClass = true;
            classType.IsPartial = true;
            return classCode;
        }

        
    }
}